#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# @Time    : 2018/12/5 22:26
# @Author  : liujiantao
# @Site    : 
# @File    : lightgbm_train01.py
# @Software: PyCharm
from tiancheng.base.base_helper import *
X_train, X_test, y_train, y_test,test, cols= get_train_vail_data(1)
# test = get_test_data()[cols]
# # 主成分分析PCA
# from sklearn.decomposition import PCA
# pca = PCA(n_components=2)
# X_train_pca = pca.fit_transform(X_train)
# X_test_pca = pca.transform(X_test)
# 用法：
# 采用 smote 算法 进行 过采样
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=2, k_neighbors=5)
X_train_res, y_train_res = sm.fit_sample(X_train, y_train.ravel())

import lightgbm as lgb
clf = lgb.LGBMClassifier(
    colsample_bytree=1.0,
    learning_rate=0.03,
    num_leaves=3000,  # 决策树的叶子节点
    max_depth=-1,
    min_child_samples=8,
    min_child_weight=0.001,
    min_split_gain=0.0,
    n_estimators=10000,
    n_jobs=-1,  # 等于-1的时候，表示cpu里的所有core进行工作。
    objective='binary',  # 目标为2分类,
    reg_alpha=0.1,
    reg_lambda=0.0,
    silent=True,
    subsample=0.95,
    subsample_for_bin=20000,
    subsample_freq=1)
clf.fit( X_train,  y_train, early_stopping_rounds=50, eval_metric="logloss",
        eval_set=[( X_test,  y_test)])
feature_name = cols

try:
    test.pop(tag_hd.Tag)
    test.pop(tag_hd.UID)
except:
    pass

feature_importance(clf, feature_name,  X_test,  y_test)
ytestPre = clf.predict_proba( X_test)[:, 1]
m = tpr_weight_funtion( y_test, ytestPre)
print(m)
sub_pre = clf.predict_proba(test.values)[:, 1]

# sub_pre = [float_to_str(item[1]) for item in sub_pre]
sub = get_sub()
sub['Tag'] = sub_pre
sub.to_csv(sub_base_path + 'lightgbm_train02_%s.csv' % str(m), index=False)